/*
 * Copyright © YOLANDA. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hks360.car_treasure.internet.request_net.nohttp.base_layer;


import com.google.gson.reflect.TypeToken;
import com.hks360.car_treasure.internet.request_net.nohttp.user_interface.HttpCallback;
import com.hks360.car_treasure.internet.request_net.nohttp.user_javabean.UserLogData;
import com.hks360.car_treasure.util.CommonUtil;
import com.hks360.car_treasure.util.GsonUtil;
import com.hks360.car_treasure.util.LogUtil;
import com.hks360.nohttp.OnResponseListener;
import com.hks360.nohttp.Response;
import com.hks360.nohttp.error.ClientError;
import com.hks360.nohttp.error.NetworkError;
import com.hks360.nohttp.error.ServerError;
import com.hks360.nohttp.error.TimeoutError;
import com.hks360.nohttp.error.URLError;
import com.hks360.nohttp.error.UnKnownHostError;

import java.util.List;


//NoHttp回调与自己的监听接口对接
public class HttpResponseListener<T> implements OnResponseListener<T> {
	private HttpCallback<T> callback;//结果回调
	public HttpResponseListener(HttpCallback<T> httpCallback) {
		this.callback = httpCallback;
	}
	//开始请求
	@Override
	public void onStart(int what) {
		if (callback != null) {
			callback.onStart(what);
		}
	}
	//结束请求
	@Override
	public void onFinish(int what) {
		if(callback != null) {
			callback.onFinish(what);
		}
	}
	//成功回调
	@Override
	public void onSucceed(int what, Response<T> response) {
		LogUtil.i("resp_result-->" + response.get());
		if (callback != null)
		{
				callback.onSuccess(what,response);
		}

	}
	//失败回调
	@Override
	public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
		if (exception instanceof ClientError) {// 客户端错误
			CommonUtil.showToast("客户端发生错误");
		} else if (exception instanceof ServerError) {// 服务器错误
			CommonUtil.showToast("服务器发生错误");
		} else if (exception instanceof NetworkError) {// 网络不好
			CommonUtil.showToast("请检查网络");
		} else if (exception instanceof TimeoutError) {// 请求超时
			CommonUtil.showToast("请求超时，网络不好或者服务器不稳定");
		} else if (exception instanceof UnKnownHostError) {// 找不到服务器
			CommonUtil.showToast("未发现指定服务器");
		} else if (exception instanceof URLError) {// URL是错的
			CommonUtil.showToast("URL错误");
		} else {
			CommonUtil.showToast("未知错误");
		}
		LogUtil.e("错误：" + exception.getMessage());
		if (callback != null)
			callback.onFailed(what);
	}

}
